home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / fg / fgmisc10 / char.c < prev    next >
Text File  |  1993-10-25  |  12KB  |  451 lines

  1. /****************************************************************************\
  2. *                                                                            *
  3. *  char.c -- character display and management functions for use with the     *
  4. *  menu code and other programs.                                             *
  5. *                                                                            *
  6. \****************************************************************************/
  7.  
  8. #include "defs.h"
  9.  
  10. /****************************************************************************\
  11. *                                                                            *
  12. *  put_btext -- put 5X5 bitmapped characters on the screen.                  *
  13. *  for better fonts, use Fastgraph/Fonts!                                    *
  14. *                                                                            *
  15. \****************************************************************************/
  16.  
  17. char chars[43][5] = {
  18.    0,   0,   0,   0,   0,
  19. -120,  -8,-120,  80,  32,
  20.  -16,-120, -16,-120, -16,
  21.  120,-128,-128,-128, 120,
  22.  -16,-120,-120,-120, -16,
  23.   -8,-128, -16,-128,  -8,
  24. -128,-128, -16,-128, -16,
  25.  112,-120,-104,-128, 120,
  26. -120,-120,  -8,-120,-120,
  27.  112,  32,  32,  32, 112,
  28.   96,-112,  16,  16,  56,
  29. -120,-112, -32,-112,-120,
  30.   -8,-128,-128,-128,-128,
  31. -120, -88, -88, -40,-120,
  32. -120,-104, -88, -56,-120,
  33.  112,-120,-120,-120, 112,
  34. -128,-128, -16,-120, -16,
  35.  120, -88,-120,-120, 112,
  36. -112, -96, -16,-120, -16,
  37.  -16,   8, 112,-128, 120,
  38.   32,  32,  32,  32,  -8,
  39.  112,-120,-120,-120,-120,
  40.   32,  80,-120,-120,-120,
  41. -120, -40, -88, -88,-120,
  42. -120,  80,  32,  80,-120,
  43.   32,  32,  32,  80,-120,
  44.   -8,  64,  32,  16,  -8,
  45.  112,-120,-120,-120, 112,
  46.  112,  32,  32,  96,  32,
  47.  -16,  64,  32,-112,  96,
  48.  -32,  16,  96,  16, -32,
  49.   16,  16, -16,-112,-112,
  50.  112,   8, -16,-128,  -8,
  51.  112,-120, -16,-128, 112,
  52.   64,  64,  32,  16,  -8,
  53.  112,-120, 112,-120, 112,
  54.   16,   8, 120,-120, 112,
  55.  128,  64,   0,   0,   0,
  56.   64,   0,   0,   0,   0,
  57.    0,   0, 112,   0,   0,
  58.   64,   0,   0,  64,   0,
  59.   16,   0,  16,  72,  48,
  60.    0, 112,   0, 112,   0
  61. };
  62.  
  63. void put_bstring(char *string,int ix,int iy)
  64. {
  65.    register int i;
  66.    int nchar;
  67.    int blank;
  68.    char ch;
  69.  
  70.    nchar = strlen(string);
  71.  
  72.    for (i = 0; i < nchar; i++)
  73.    {
  74.       blank = FALSE;
  75.       ch = string[i];
  76.  
  77.       /* upper case */
  78.  
  79.       if (ch >= 65 && ch <= 90)
  80.          ch -= 64;
  81.  
  82.       /* lower case */
  83.  
  84.       else if (ch >= 97 && ch <= 122)
  85.          ch -= 96;
  86.  
  87.       /* numbers */
  88.  
  89.       else if (ch >= 48 && ch <= 57)
  90.          ch -= 21;
  91.  
  92.       /* comma */
  93.  
  94.       else if (ch == 44)
  95.          ch = 37;
  96.  
  97.       /* period */
  98.  
  99.       else if (ch == 46)
  100.          ch = 38;
  101.  
  102.       /* minus */
  103.  
  104.       else if (ch == 45)
  105.          ch = 39;
  106.  
  107.      /* colon */
  108.  
  109.       else if (ch == 58)
  110.          ch = 40;
  111.  
  112.      /* question mark */
  113.  
  114.       else if (ch == 63)
  115.          ch = 41;
  116.  
  117.      /* equals */
  118.  
  119.       else if (ch == 61)
  120.          ch = 42;
  121.  
  122.       else
  123.          blank = TRUE;
  124.  
  125.       fg_move(ix,iy);
  126.       if (!blank) fg_drawmap(&chars[ch][0],1,5);
  127.       ix += 6;
  128.    }
  129. }
  130.  
  131. /****************************************************************************\
  132. *                                                                            *
  133. *  center_bstring -- display the bitmapped font, centered                    *
  134. *                                                                            *
  135. \****************************************************************************/
  136.  
  137. void center_bstring(char *string,int x1,int x2,int y)
  138. {
  139.    int x;
  140.  
  141.    x = get_center(string,x1,x2);
  142.    put_bstring(string,x,y);
  143. }
  144.  
  145. /****************************************************************************\
  146. *                                                                            *
  147. *  erase_char -- erase a single 5x5 character                                *
  148. *                                                                            *
  149. \****************************************************************************/
  150.  
  151. void erase_char(int x,int y)
  152. {
  153.    register int color;
  154.  
  155.    color = fg_getcolor();
  156.    fg_setcolor(white);
  157.    fg_rect(x,x+5,y-5,y);
  158.    fg_setcolor(color);
  159. }
  160.  
  161. /****************************************************************************\
  162. *                                                                            *
  163. *  get_center -- determine the center of a string                            *
  164. *                                                                            *
  165. \****************************************************************************/
  166.  
  167. get_center(char *string,int x1,int x2)
  168. {
  169.    int nchar;
  170.  
  171.    nchar = strlen(string);
  172.    return(((x1 + x2) / 2) - nchar*3);
  173. }
  174.  
  175. /****************************************************************************\
  176. *                                                                            *
  177. *  put_char -- put a 1 character string                                      *
  178. *                                                                            *
  179. \****************************************************************************/
  180.  
  181. void put_char(unsigned char key,int x,int y)
  182. {
  183.    char string[2];
  184.    string[1] = '\0';
  185.    string[0] = key;
  186.    put_bstring(string,x,y);
  187. }
  188.  
  189. /****************************************************************************\
  190. *                                                                            *
  191. *  put_cursor -- a little line that goes under the character                 *
  192. *                                                                            *
  193. \****************************************************************************/
  194.  
  195. void put_cursor(int x,int y,int cursor_color)
  196. {
  197.    register int color;
  198.  
  199.    color = fg_getcolor();
  200.    fg_setcolor(cursor_color);
  201.    fg_rect(x,x+5,y,y);
  202.    fg_setcolor(color);
  203. }
  204.  
  205. /****************************************************************************\
  206. *                                                                            *
  207. *  file_help_screen -- pop help for load file screen                         *
  208. *                                                                            *
  209. \****************************************************************************/
  210.  
  211. void file_help_screen()
  212. {
  213.    register int i;
  214.    int y;
  215.    static char *string[] =
  216.    {
  217.       "    Popup Help",
  218.       " ",
  219.       "Arrows: Move between fields",
  220.       "PGUP:   Next thing",
  221.       "PGDN:   Prev thing",
  222.       "INS:    Insert thing",
  223.       "DEL:    Delete thing",
  224.       "F2:     F2 thing",
  225.       "F10:    F10 thing",
  226.       "Esc:    Abort"
  227.    };
  228.  
  229.    fg_setpage(0);
  230.    fg_mousevis(0);
  231.    fg_save(48,271,40,170);
  232.  
  233.    fg_setcolor(white);
  234.    fg_rect(56,263,45,165);
  235.    fg_setcolor(black);
  236.    fg_box(56,263,45,165);
  237.  
  238.    y = 60;
  239.    for (i = 0; i < 10; i++)
  240.    {
  241.       put_bstring(string[i], 80,y);
  242.       y += 10;
  243.    }
  244.  
  245.    fg_waitkey();
  246.    fg_restore(48,271,40,170);
  247.    fg_setpage(1);
  248.    fg_setcolor(blue);
  249.    fg_rect(48,271,40,170);
  250.    fg_setpage(0);
  251. }
  252.  
  253. /****************************************************************************\
  254. *                                                                            *
  255. *  get_field get character string, allow for up arrows and down arrows       *
  256. *                                                                            *
  257. \****************************************************************************/
  258.  
  259. get_field(char *string,int x,int y,int max_length,unsigned char key,unsigned char aux)
  260. {
  261.    register int i;
  262.    int color;
  263.    int cursor_timer;
  264.    int foreground;
  265.    int background;
  266.    int xmax, ymin;
  267.    int first;
  268.  
  269.    first = TRUE;
  270.  
  271.    foreground = fg_getcolor();
  272.    background = white;
  273.  
  274.    xmax = x + 6*max_length;
  275.    ymin = y - 6;
  276.  
  277.    i = 0;
  278.    cursor_timer = 16;
  279.    color = foreground;
  280.    fg_setcolor(foreground);
  281.  
  282.    for (;;)
  283.    {
  284.       cursor_timer--;
  285.       if (cursor_timer == 8)
  286.          color = background;
  287.       else if (cursor_timer == 0)
  288.       {
  289.          cursor_timer = 16;
  290.          color = foreground;
  291.       }
  292.       if (i < max_length) put_cursor(x,y+1,color);
  293.       if (key+aux > 0)
  294.          if (i < max_length) put_cursor(x,y+1,background);
  295.  
  296.       if (i == 0 && islower(key)) key ^= 32;
  297.  
  298.       if ((isalnum(key) || key == SPACE || ispunct(key)) && i < max_length)
  299.       {
  300.          if (first)
  301.          {
  302.             string[i] = '\0';
  303.             fg_setcolor(background);
  304.             fg_rect(x-2,xmax+1,ymin,y+1);
  305.             first = FALSE;
  306.             fg_setcolor(foreground);
  307.          }
  308.  
  309.          put_cursor(x,y+1,background);
  310.          put_char(key,x,y);
  311.          x += 6;
  312.          string[i++] = key;
  313.          string[i] = '\0';
  314.       }
  315.  
  316.       else if (key == BS && i > 0)
  317.       {
  318.          if (i < max_length) put_cursor(x,y+1,background);
  319.          x -= 6;
  320.          erase_char(x,y);
  321.          i--;
  322.          string[i] = '\0';
  323.       }
  324.  
  325.       else if (key == ESC)
  326.          return(ESC);
  327.       else if (aux == UP_ARROW)
  328.          return(UP_ARROW);
  329.       else if (aux == DOWN_ARROW)
  330.          return(DOWN_ARROW);
  331.       else if (aux == PGDN)
  332.          return(PGDN);
  333.       else if (aux == PGUP)
  334.          return(PGUP);
  335.       else if (aux == LEFT_ARROW)
  336.          return(LEFT_ARROW);
  337.       else if (aux == RIGHT_ARROW)
  338.          return(RIGHT_ARROW);
  339.       else if (key == ENTER)
  340.          return(ENTER);
  341.       else if (aux == INSERT)
  342.          return(INSERT);
  343.       else if (aux == DELETE)
  344.          return(DELETE);
  345.       else if (aux == F10)
  346.          return(F10);
  347.       else if (aux == F2)
  348.          return(F2);
  349.       else if (aux == F1)
  350.          file_help_screen();
  351.  
  352.       fg_waitfor(1);
  353.       fg_intkey(&key,&aux);
  354.    }
  355. }
  356.  
  357. /****************************************************************************\
  358. *                                                                            *
  359. *  get_string -- accept and display a character string                       *
  360. *                                                                            *
  361. \****************************************************************************/
  362.  
  363. get_string(char *string, int x, int y, int max_length)
  364. {
  365.    register int i;
  366.    int color;
  367.    int cursor_timer;
  368.    int foreground;
  369.    int background;
  370.    int xmax, ymin;
  371.    unsigned char key,aux;
  372.  
  373.    foreground = fg_getcolor();
  374.    background = white;
  375.  
  376.    /* first keystroke begins text input */
  377.  
  378.    fg_getkey(&key,&aux);
  379.  
  380.    /* clear the input area */
  381.  
  382.    fg_setcolor(background);
  383.    xmax = x + 6*max_length;
  384.    ymin = y - 6;
  385.    fg_rect(x-2,xmax+1,ymin,y+1);
  386.  
  387.    /* initialize the string and cursor */
  388.  
  389.    i = 0;
  390.    string[0] = '\0';
  391.    cursor_timer = 16;
  392.    color = foreground;
  393.    fg_setcolor(foreground);
  394.  
  395.    do
  396.    {
  397.       /* alternate the cursor color - make it blink */
  398.  
  399.       cursor_timer--;
  400.       if (cursor_timer == 8)
  401.          color = background;
  402.       else if (cursor_timer == 0)
  403.       {
  404.          cursor_timer = 16;
  405.          color = foreground;
  406.       }
  407.       if (i < max_length) put_cursor(x,y,color);
  408.  
  409.       if (i == 0 && islower(key)) key ^= 32;
  410.  
  411.       /* check if it is a printable character */
  412.  
  413.       if ((isalnum(key) || key == SPACE || ispunct(key)) && i < max_length)
  414.       {
  415.          put_cursor(x,y,background);
  416.          put_char(key,x,y);
  417.          x += 6;
  418.          string[i++] = key;
  419.       }
  420.  
  421.       /* if it is backspace, erase the character and back up one */
  422.  
  423.       else if (key == BS && i > 0)
  424.       {
  425.          if (i < max_length) put_cursor(x,y,background);
  426.          x -= 6;
  427.          erase_char(x,y);
  428.          i--;
  429.       }
  430.  
  431.       /* pressing escape bounces you right out of here */
  432.  
  433.       else if (key == ESC)
  434.       {
  435.          if (i < max_length) put_cursor(x,y,background);
  436.             string[i] = '\0';
  437.          return(ESC);
  438.       }
  439.  
  440.       /* get another key */
  441.  
  442.       fg_waitfor(1);
  443.       fg_intkey(&key,&aux);
  444.    }
  445.    while (key != ENTER);
  446.  
  447.    if (i < max_length) put_cursor(x,y,background);
  448.    string[i] = '\0';
  449.    return(0);
  450. }
  451.